/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package baselinetagging;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


/**
 *
 * @author Nguyen Thuy Ngoc <ntngoc1988@gmail.com>
 */
public class XmlParser {
    DocumentBuilderFactory factory;
    public XmlParser()throws SAXException, IOException, ParserConfigurationException{
        factory = DocumentBuilderFactory.newInstance();
    }

    private String convertNormalSentence(String sentence){
        String result = "";
        String[] parts = sentence.split(" ");
        for(int i = 0; i < parts.length; i++) {
            int index = parts[i].indexOf("/");
            if(i == parts.length - 1){
                result += parts[i].substring(0, index);
            }
            else{
                result += parts[i].substring(0, index) + " ";
            }     
        }
        return result;
    }

    private ArrayList<String> readStringFromXml(File fileName) {
        Document dom = null;
        try{
            dom = factory.newDocumentBuilder().parse(fileName);
        }
        catch (Exception error) {
            error.printStackTrace();
        }
        Element docElement = dom.getDocumentElement();
        NodeList nl = docElement.getElementsByTagName("SEG");
        ArrayList<String> results = new ArrayList<String>();
        if(nl != null){
            for(int i = 0; i < nl.getLength(); i++){
                Node node = nl.item(i);
                results.add(convertNormalSentence(node.getTextContent()));
            }
        }
        return results;
    }

    /*public String[] loadRawSentencesFromFolder(String folderName) {
        List<String> rawSentences = new ArrayList<String>();

        File folder = new File(folderName);
        for (File file : folder.listFiles()) {
            String filename = file.getAbsolutePath();
        }

        return rawSentences.toArray(new String[0]);
    }*/

    public ArrayList<String> loadFolder(String folderName) {
        ArrayList<String> results = new ArrayList<String>();
        File directory = new File(folderName);
        File[] fileChildren = directory.listFiles();
        for(int i = 0; i < fileChildren.length; i++) {
            ArrayList<String> textOfFile = readStringFromXml(fileChildren[i]);
            results.addAll(textOfFile);
        }
        return results;
    }


    public ArrayList<String> getRawData(ArrayList<String> sentences) {
        ArrayList<String> results = new ArrayList<String>();
        for(int i = 0; i < sentences.size(); i++){
            results.add(sentences.get(i).replace("_", " "));
        }
        return results;
    }

    public GoldenSentence buildGoldenFromNormalSetence(String normalSentence) {
        return null;
    }

    public GoldenSentence buildGoldenFromTruthSetence(String truthSentence) {
        return null;
    }
}
